package cc.rengu.igas.channel.upgw.core.service.trans;

import cc.rengu.igas.channel.upgw.core.service.base.UpgwBaseService;
import cc.rengu.igas.channel.upgw.common.constant.UpgwHeaderConstant;
import cc.rengu.igas.channel.upgw.common.constant.UpgwParamConstant;
import cc.rengu.igas.channel.upgw.common.constant.UpgwTreeNodeConstant;
import cc.rengu.igas.channel.upgw.common.enums.UpgwRespCodeEnum;
import cc.rengu.igas.channel.upgw.facade.bean.UpgwMchntInfo;
import cc.rengu.igas.channel.upgw.facade.request.UpgwEnterpriseGatewayFullPaymentRefundRequest;
import cc.rengu.igas.channel.upgw.facade.response.UpgwEnterpriseGatewayFullPaymentRefundResponse;
import cc.rengu.jradp.schedule.ScheduleImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

/**
 * @author wukefan
 * @version 1.0.0
 * @date 2020/10/6 20:30
 */
public class UpgwEnterpriseGatewayFullPaymentRefundService extends UpgwBaseService {

    @Override
    protected UpgwEnterpriseGatewayFullPaymentRefundRequest beforeBizProcess() throws Exception {
        UpgwEnterpriseGatewayFullPaymentRefundRequest request = new UpgwEnterpriseGatewayFullPaymentRefundRequest();
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {

        XmlTreeUtil xmlTree = new XmlTreeUtil();
        UpgwEnterpriseGatewayFullPaymentRefundRequest upgwEnterpriseGatewayFullPaymentRefundRequest = (UpgwEnterpriseGatewayFullPaymentRefundRequest) request;
        BizResponse<UpgwEnterpriseGatewayFullPaymentRefundResponse> bizResponse = new BizResponse<>();
        UpgwEnterpriseGatewayFullPaymentRefundResponse response = new UpgwEnterpriseGatewayFullPaymentRefundResponse();
        if (!verifyRequest(upgwEnterpriseGatewayFullPaymentRefundRequest)) {
            response.setRespCode(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespCode());
            response.setRespDesc(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespDesc());
            bizResponse.setResult(response);
            return bizResponse;
        }

        /* 将请求赋值到xml中 */
        requestToXMLTree(xmlTree, upgwEnterpriseGatewayFullPaymentRefundRequest);

        /* TODO 演示环境默认返回成功 START */
        if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
            rglog.info("演示环境默认返回成功");
            response.setRespCode(UpgwRespCodeEnum.ACCEPT_SUCCESS.getRespCode());
            response.setRespDesc(UpgwRespCodeEnum.ACCEPT_SUCCESS.getRespDesc());
            bizResponse.setResult(response);
            return bizResponse;
        }
        /* TODO 演示环境默认返回成功 END */

        try {
            int retCode = schedule.callService(UpgwParamConstant.UPGW_COMMUNICATION_SVC_NM, UpgwParamConstant.UPGW_COMMUNICATION_SVC_LABEL);
            /*判断retCode返回值*/
            /*通讯成功*/
            /*通讯成功*/
            if (ScheduleImpl.SERVICE_SUCCESS == retCode) {
                /*判断状态码*/
                String status = xmlTree.getXmlTreeStringValue(UpgwHeaderConstant.STATUS);
                if (UpgwParamConstant.STATUS_SUCC.equals(status)) {
                    rglog.debug("调用通道业务成功");
                    //通道调用成功的应答处理
                    //成功 转换成功应答码为统一应答码，调用方根据返回00000000判断业务是否成功
                    response.setRespCode(UpgwRespCodeEnum.ACCEPT_SUCCESS.getRespCode());
                    response.setRespDesc(UpgwRespCodeEnum.ACCEPT_SUCCESS.getRespDesc());
                    bizResponse.setResult(response);
                } else if (UpgwParamConstant.STATUS_FAIL.equals(status)) {
                    rglog.debug("调用通道业务失败");
                    //通过判断应答码，判定交易状态是否为超时;ESB超时错误码为E000A703
                    String code = xmlTree.getXmlTreeStringValue(UpgwHeaderConstant.CODE);
                    if (StringUtil.isNullorEmpty(code)) {
                        response.setRespCode(UpgwParamConstant.DEFAULT_ERR_CODE);
                    } else if (UpgwParamConstant.ESB_TIME_OUT_E000A703.equals(code)) {
                        response.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                    } else {
                        response.setRespCode(code);
                    }
                    String desc = xmlTree.getXmlTreeStringValue(UpgwHeaderConstant.DESC);
                    if (StringUtil.isNullorEmpty(desc)) {
                        response.setRespDesc(UpgwParamConstant.DEFAULT_ERR_DESC);
                    } else {
                        response.setRespDesc(desc);
                    }
                    bizResponse.setResult(response);
                } else {
                    rglog.debug("调用通道业务未知");
                    String code = xmlTree.getXmlTreeStringValue(UpgwHeaderConstant.CODE);
                    if (StringUtil.isNullorEmpty(code)) {
                        response.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                    } else {
                        response.setRespCode(code);
                    }
                    String desc = xmlTree.getXmlTreeStringValue(UpgwHeaderConstant.DESC);
                    if (StringUtil.isNullorEmpty(desc)) {
                        response.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                    } else {
                        response.setRespDesc(desc);
                    }
                    bizResponse.setResult(response);
                }
            } else if (ScheduleImpl.SERVICE_TIMEOUT == retCode) {
                //调用超时 赋值应答
                rglog.error("callService {} 失败！返回值={}", UpgwParamConstant.UPGW_COMMUNICATION_SVC_NM, retCode);
                response.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                response.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
                bizResponse.setResult(response);
                return bizResponse;
            } else {
                //调用明确失败
                rglog.error("callService {} 失败！返回值={}", UpgwParamConstant.UPGW_COMMUNICATION_SVC_NM, retCode);
                response.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
                response.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
                bizResponse.setResult(response);
                return bizResponse;
            }
        } catch (Exception e) {
            //失败赋值应答码为明确失败及应答消息直接退出
            rglog.error("{}", StringUtil.ExceptionToString(e));
            response.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
            response.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            bizResponse.setResult(response);
            return bizResponse;
        }
        bizResponse.setResult(response);
        return bizResponse;
    }

    private void requestToXMLTree(XmlTreeUtil xmlTree, UpgwEnterpriseGatewayFullPaymentRefundRequest upgwEnterpriseGatewayFullPaymentRefundRequest) throws Exception {
        /*公共报文信息*/
        /*请求方机构代码*/
        xmlTree.setXmlTreeStringValue(UpgwHeaderConstant.BRANCH_ID, upgwEnterpriseGatewayFullPaymentRefundRequest.getHeader().getInstId() + UpgwParamConstant.DEFAULT_VIRTUAL_INST_ID);
        /*服务渠道代码*/
        xmlTree.setXmlTreeStringValue(UpgwHeaderConstant.CHANNEL_ID, UpgwParamConstant.CHANNEL_ID);
        if (!StringUtil.isNullorEmpty(upgwEnterpriseGatewayFullPaymentRefundRequest.getHeader().getFrontChannelId())) {
            xmlTree.setXmlTreeStringValue(UpgwHeaderConstant.CHANNEL_ID, upgwEnterpriseGatewayFullPaymentRefundRequest.getHeader().getFrontChannelId());
        }
        /*请求方系统代码*/
        xmlTree.setXmlTreeStringValue(UpgwHeaderConstant.REQUESTER_ID, UpgwParamConstant.REQUEST_ID);
        /*ESB服务码*/
        xmlTree.setXmlTreeStringValue(UpgwHeaderConstant.SERVICE_ID, upgwEnterpriseGatewayFullPaymentRefundRequest.getHeader().getTxnNum());
        /*请求方服务流水号*/
        xmlTree.setXmlTreeStringValue(UpgwHeaderConstant.SERVICE_SN, upgwEnterpriseGatewayFullPaymentRefundRequest.getHeader().getTraceNo());
        /*请求方服务时间*/
        xmlTree.setXmlTreeStringValue(UpgwHeaderConstant.SERVICE_TIME, upgwEnterpriseGatewayFullPaymentRefundRequest.getHeader().getTransDate() + upgwEnterpriseGatewayFullPaymentRefundRequest.getHeader().getTransTime());
        /*版本号*/
        xmlTree.setXmlTreeStringValue(UpgwHeaderConstant.VERSION_ID, UpgwParamConstant.VERSION_ID);

        /*业务种类*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.BIZTP, upgwEnterpriseGatewayFullPaymentRefundRequest.getBusiType());
        /*业务功能*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.BIZFUNC, UpgwParamConstant.BIZ_FUNC_UPGW_FULL_PAYMENT_REFUND);
        /*初始交易类型*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.INITRXTP, UpgwParamConstant.INITRXTP_ENTERPRISE);
        /*交易金额*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.TRXAMT, AmountUtil.changeY2F(upgwEnterpriseGatewayFullPaymentRefundRequest.getOrderAmt()));
        /*交易终端类型*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.TRXINF_TRXTRMTP, upgwEnterpriseGatewayFullPaymentRefundRequest.getTermType());
        /*原ESB流水*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.ORIGIN_SERVICE_SN, upgwEnterpriseGatewayFullPaymentRefundRequest.getOrigTraceNo());
        /*原交易时间*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.ORIGIN_SERVICE_DATE, upgwEnterpriseGatewayFullPaymentRefundRequest.getOrigTransDate());
        /*网关渠道标识*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.PYEEINF_ISSRCHNNLID, upgwEnterpriseGatewayFullPaymentRefundRequest.getPayeeIssrChannelId());
        /*收款方账户*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.PYEEINF_PYEEACCTID, upgwEnterpriseGatewayFullPaymentRefundRequest.getPayeeAcctInfo());
        /*收款方账户类型*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.PYEEINF_PYEEACCTTP, upgwEnterpriseGatewayFullPaymentRefundRequest.getPayeeAcctType());
        /*报文初始发起机构标识*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.ORIISSRID, upgwEnterpriseGatewayFullPaymentRefundRequest.getOriIssrId());

        /*商户编码*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.MRCHNTINF_MRCHNTNO, upgwEnterpriseGatewayFullPaymentRefundRequest.getMchntInfo().getMchntNo());
        /*商户类别*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.MRCHNTINF_MRCHNTTPID, upgwEnterpriseGatewayFullPaymentRefundRequest.getMchntInfo().getMchntType());
        /*商户名称*/
        xmlTree.setXmlTreeStringValue(UpgwTreeNodeConstant.MRCHNTINF_MRCHNTPLTFRMNM, upgwEnterpriseGatewayFullPaymentRefundRequest.getMchntInfo().getMchntName());
    }

    private boolean verifyRequest(UpgwEnterpriseGatewayFullPaymentRefundRequest upgwEnterpriseGatewayFullPaymentRefundRequest) {
        UpgwMchntInfo mchntInfo = upgwEnterpriseGatewayFullPaymentRefundRequest.getMchntInfo();
        if (null == mchntInfo) {
            rglog.error("mchntInfo信息为null！");
            return false;
        }
        String busiType = upgwEnterpriseGatewayFullPaymentRefundRequest.getBusiType();
        String orderAmt = upgwEnterpriseGatewayFullPaymentRefundRequest.getOrderAmt();
        String termType = upgwEnterpriseGatewayFullPaymentRefundRequest.getTermType();
        String origTraceNo = upgwEnterpriseGatewayFullPaymentRefundRequest.getOrigTraceNo();
        String origTransDate = upgwEnterpriseGatewayFullPaymentRefundRequest.getOrigTransDate();
        String payeeIssrChannelId = upgwEnterpriseGatewayFullPaymentRefundRequest.getPayeeIssrChannelId();
        String payeeAcctInfo = upgwEnterpriseGatewayFullPaymentRefundRequest.getPayeeAcctInfo();
        String payeeAcctType = upgwEnterpriseGatewayFullPaymentRefundRequest.getPayeeAcctType();
        String oriIssrId = upgwEnterpriseGatewayFullPaymentRefundRequest.getOriIssrId();
        String mchntNo = upgwEnterpriseGatewayFullPaymentRefundRequest.getMchntInfo().getMchntNo();
        String mchntType = upgwEnterpriseGatewayFullPaymentRefundRequest.getMchntInfo().getMchntType();
        String mchntName = upgwEnterpriseGatewayFullPaymentRefundRequest.getMchntInfo().getMchntName();

        if (StringUtil.isNullorEmpty(busiType) ||
                StringUtil.isNullorEmpty(orderAmt) ||
                StringUtil.isNullorEmpty(termType) ||
                StringUtil.isNullorEmpty(oriIssrId) ||
                StringUtil.isNullorEmpty(origTraceNo) ||
                StringUtil.isNullorEmpty(origTransDate) ||
                StringUtil.isNullorEmpty(payeeIssrChannelId) ||
                StringUtil.isNullorEmpty(mchntNo) ||
                StringUtil.isNullorEmpty(mchntType) ||
                StringUtil.isNullorEmpty(mchntName) ||
                StringUtil.isNullorEmpty(payeeAcctInfo) ||
                StringUtil.isNullorEmpty(payeeAcctType)) {
            //失败赋值应答码为明确失败及应答消息直接退出
            rglog.error("缺少一个或多个业务参数！");
            return false;
        }
        rglog.trace("检查请求通过");
        return true;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {
        UpgwEnterpriseGatewayFullPaymentRefundResponse upgwEnterpriseGatewayFullPaymentRefundResponse = (UpgwEnterpriseGatewayFullPaymentRefundResponse) response.getResult();
        ConvertUtil.convertInput(upgwEnterpriseGatewayFullPaymentRefundResponse);
    }
}
